package app.board;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Point;

public class Board {
	int _sizeX, _sizeY;
	short _board[][];
	Point _coordinates[][];
	int _borderWidth = 2;
	int _lineWidth = 1;
	int _fieldWidth = 30;
	int _targetWidth = 2;
	int _pointWidth = 6;

	public Board(int sizeX, int sizeY) {
		_sizeX = sizeX + 3;
		_sizeY = sizeY + 1;
		_prepareBoardArray();
		_prepareCoordinatesArray();
		// printArrayPoint(_coordinates);
	}

	public int getRealSizeX() {
		return _sizeX;
	}

	public int getRealSizeY() {
		return _sizeY;
	}

	private void _prepareBoardArray() {
		_board = new short[_sizeX][_sizeY];

		for (int i = 0; i < _sizeX; i++) {
			for (int j = 0; j < _sizeY; j++) {
				_board[0][0] = 0;
			}
		}

		// corners
		_board[0][1] = 247;
		_board[0][_sizeY - 2] = 253;
		_board[_sizeX - 1][1] = 223;
		_board[_sizeX - 1][_sizeY - 2] = 127;

		// left line
		for (int i = 2; i < _sizeY - 2; i++) {
			_board[0][i] = 241;
		}
		// right line
		for (int i = 2; i < _sizeY - 2; i++) {
			_board[_sizeX - 1][i] = 31;
		}

		// top line
		int targetLeft = (_sizeX - _targetWidth) / 2;
		for (int i = 1; i < targetLeft; i++) {
			_board[i][1] = 1 + 2 + 4 + 64 + 128;
		}
		for (int i = targetLeft + 1 + _targetWidth; i < _sizeX - 1; i++) {
			_board[i][1] = 1 + 2 + 4 + 64 + 128;
		}

		// bottom line
		for (int i = 1; i < targetLeft; i++) {
			_board[i][_sizeY - 2] = 4 + 8 + 16 + 32 + 64;
		}
		for (int i = targetLeft + 1 + _targetWidth; i < _sizeX - 1; i++) {
			_board[i][_sizeY - 2] = 4 + 8 + 16 + 32 + 64;
		}

		// top target
		_board[targetLeft][0] = 247;
		_board[targetLeft + 2][0] = 253;
		_board[targetLeft][1] = 1 + 64 + 128;
		_board[targetLeft + 2][1] = 1 + 2 + 4;
		for (int i = targetLeft + 1; i < targetLeft + _targetWidth; i++) {
			_board[i][0] = 199;
		}
		// bottom target
		_board[targetLeft][_sizeY - 2] = 16 + 32 + 64;
		_board[targetLeft + 2][_sizeY - 2] = 4 + 8 + 16;
		_board[targetLeft][_sizeY - 1] = 253;
		_board[targetLeft + 2][_sizeY - 1] = 127;
		for (int i = targetLeft + 1; i < targetLeft + _targetWidth; i++) {
			_board[i][_sizeY - 1] = 124;
		}

		// outside of game field
		for (int i = 0; i < targetLeft; i++) {
			_board[i][0] = 255;
		}
		for (int i = targetLeft + _targetWidth + 1; i < _sizeX; i++) {
			_board[i][0] = 255;
		}
		for (int i = 0; i < targetLeft; i++) {
			_board[i][_sizeY - 1] = 255;
		}
		for (int i = targetLeft + _targetWidth + 1; i < _sizeX; i++) {
			_board[i][_sizeY - 1] = 255;
		}
	}

	public void printArray(short array[][]) {
		// System.out.println(Arrays.deepToString(_board));
		for (int i = 0; i < _sizeY; i++) {
			for (int j = 0; j < _sizeX; j++) {
				System.out.print(array[j][i] + "\t");
			}
			System.out.println();
		}
	}

	public void printArrayPoint(Point array[][]) {
		// System.out.println(Arrays.deepToString(_board));
		for (int i = 0; i < _sizeY; i++) {
			for (int j = 0; j < _sizeX; j++) {
				System.out.print(array[j][i].getX() + "," + array[j][i].getY()
						+ "\t");
			}
			System.out.println();
		}
	}

	private void _prepareCoordinatesArray() {
		_coordinates = new Point[_sizeX][_sizeY];

		int currentPointX = 1;

		for (int i = 0; i < _sizeX; i++) {
			int currentPointY = 1;
			for (int j = 0; j < _sizeY; j++) {
				_coordinates[i][j] = new Point(currentPointX, currentPointY);
				currentPointY += _fieldWidth + _lineWidth;
			}
			currentPointX += _fieldWidth + _lineWidth;

		}
	}

	public Point getClosestPoint(int x, int y) {
		int k = -1, l = -1;
		for (int i = 0; i < _sizeX; i++) {
			if (_coordinates[i][0].getX() - 10 < x
					&& _coordinates[i][0].getX() + 10 > x) {
				k = i;
				break;
			}
		}

		for (int j = 0; j < _sizeY; j++) {
			if (_coordinates[0][j].getY() - 10 < y
					&& _coordinates[0][j].getY() + 10 > y) {
				l = j;
				break;
			}
		}

		if (k == -1 || l == -1)
			return null;

		// System.out.println("i=" + k + ", j=" + l);
		return _coordinates[k][l];
	}

	public void drawBoard(Graphics g) {
		// // left line
		// g.drawLine((int) _coordinates[0][1].getX(),
		// (int) _coordinates[0][1].getY(),
		// (int) _coordinates[0][_sizeY - 2].getX(),
		// (int) _coordinates[0][_sizeY - 2].getY());
		// // right line
		// g.drawLine((int) _coordinates[_sizeX - 1][1].getX(),
		// (int) _coordinates[_sizeX - 1][1].getY(),
		// (int) _coordinates[_sizeX - 1][_sizeY - 2].getX(),
		// (int) _coordinates[_sizeX - 1][_sizeY - 2].getY());
		//
		// // top line
		int targetLeft = (_sizeX - _targetWidth) / 2;
		// g.drawLine((int) _coordinates[0][1].getX(),
		// (int) _coordinates[0][1].getY(),
		// (int) _coordinates[targetLeft][1].getX(),
		// (int) _coordinates[targetLeft][1].getY());
		// g.drawLine((int) _coordinates[targetLeft + _targetWidth][1].getX(),
		// (int) _coordinates[targetLeft + _targetWidth][1].getY(),
		// (int) _coordinates[_sizeX - 1][1].getX(),
		// (int) _coordinates[_sizeX - 1][1].getY());
		// // bottom line
		// g.drawLine((int) _coordinates[0][_sizeY - 2].getX(),
		// (int) _coordinates[0][_sizeY - 2].getY(),
		// (int) _coordinates[targetLeft][_sizeY - 2].getX(),
		// (int) _coordinates[targetLeft][_sizeY - 2].getY());
		// g.drawLine((int) _coordinates[targetLeft + _targetWidth][_sizeY - 2]
		// .getX(),
		// (int) _coordinates[targetLeft + _targetWidth][_sizeY - 2]
		// .getY(), (int) _coordinates[_sizeX - 1][_sizeY - 2]
		// .getX(), (int) _coordinates[_sizeX - 1][_sizeY - 2]
		// .getY());
		//
		// // top target
		// g.drawLine((int) _coordinates[targetLeft][1].getX(),
		// (int) _coordinates[targetLeft][1].getY(),
		// (int) _coordinates[targetLeft][0].getX(),
		// (int) _coordinates[targetLeft][0].getY());
		// g.drawLine((int) _coordinates[targetLeft][0].getX(),
		// (int) _coordinates[targetLeft][0].getY(),
		// (int) _coordinates[targetLeft + _targetWidth][0].getX(),
		// (int) _coordinates[targetLeft + _targetWidth][0].getY());
		// g.drawLine((int) _coordinates[targetLeft + _targetWidth][0].getX(),
		// (int) _coordinates[targetLeft + _targetWidth][0].getY(),
		// (int) _coordinates[targetLeft + _targetWidth][1].getX(),
		// (int) _coordinates[targetLeft + _targetWidth][1].getY());
		//
		// // bottom target
		// g.drawLine((int) _coordinates[targetLeft][_sizeY - 2].getX(),
		// (int) _coordinates[targetLeft][_sizeY - 2].getY(),
		// (int) _coordinates[targetLeft][_sizeY - 1].getX(),
		// (int) _coordinates[targetLeft][_sizeY - 1].getY());
		// g.drawLine((int) _coordinates[targetLeft][_sizeY - 1].getX(),
		// (int) _coordinates[targetLeft][_sizeY - 1].getY(),
		// (int) _coordinates[targetLeft + _targetWidth][_sizeY - 1]
		// .getX(),
		// (int) _coordinates[targetLeft + _targetWidth][_sizeY - 1]
		// .getY());
		// g.drawLine((int) _coordinates[targetLeft + _targetWidth][_sizeY - 1]
		// .getX(),
		// (int) _coordinates[targetLeft + _targetWidth][_sizeY - 1]
		// .getY(),
		// (int) _coordinates[targetLeft + _targetWidth][_sizeY - 2]
		// .getX(),
		// (int) _coordinates[targetLeft + _targetWidth][_sizeY - 2]
		// .getY());
		g.setColor(Color.LIGHT_GRAY);

		// coloring middle of board
		int height = (int) (_coordinates[0][_sizeY - 2].getY() - _coordinates[0][1]
				.getY());
		int width = (int) (_coordinates[_sizeX - 1][0].getX() - _coordinates[0][0]
				.getX());
		g.fillRect((int) _coordinates[0][1].getX() + 1,
				(int) _coordinates[0][1].getY() + 1, width, height);

		// coloring top target
		g.fillRect((int) _coordinates[targetLeft][0].getX() + 1,
				(int) _coordinates[targetLeft][0].getY() + 1, _fieldWidth * 2,
				_fieldWidth);

		// coloring bottom target
		g.fillRect((int) _coordinates[targetLeft][_sizeY - 2].getX() + 1,
				(int) _coordinates[targetLeft][_sizeY - 2].getY() + 2,
				_fieldWidth * 2, _fieldWidth);

		g.setColor(Color.BLACK);

		for (int i = 0; i < _sizeX; i++) {
			for (int j = 0; j < _sizeY; j++) {
				g.fillArc((int) _coordinates[i][j].getX() - _pointWidth / 2,
						(int) _coordinates[i][j].getY() - _pointWidth / 2,
						_pointWidth, _pointWidth, 0, 360);
			}
		}
	}
}
